package uk.ac.sanger;


import java.sql.*; // JDBC stuff.
import java.io.*;  // Reading user input.

 public class Database
 {
         /* This is the driving engine of the program. It parses the
          * command-line arguments and calls the appropriate methods in
          * the other classes.
          *
          * You should edit this file in two ways:
          *         1)         Insert your database username and password (no @medic1!)
          *                in the proper places.
          *        2)        Implement the three functions getInformation, registerStudent
          *                and unregisterStudent.
          */
         public static void main(String[] args)
         {
                 if (args.length == 0) {
                         try {
                        	    // Class.forName("com.mysql.jdbc.Driver").newInstance();
                        	     //Class.forName("com.mysql.jdbc.Driver");
                        	     //Connection con = DriverManager.getConnection(url,"root", "");
                                 //DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
                                 String url = "ensdb-1-07.internal.sanger.ac.uk";
                                 String userName = "admin"; // Your username goes here!
                                 String password = "-pensembl"; // Your password goes here!
                                 Connection conn = DriverManager.getConnection(url,userName,password);

                                 String student = args[0]; // This is the identifier for the student.
                                 BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
                                 System.out.println("Welcome!");
                                 while(true) {
                                         System.out.println("Please choose a mode of operation:");
                                         System.out.print("? > ");
                                         String mode = input.readLine();
                                         if ((new String("information")).startsWith(mode.toLowerCase())) {
                                                 /* Information mode */
                                                getInformation(conn, student);
                                         } 
                                         
                                         else if ((new String("quit")).startsWith(mode.toLowerCase())) {
                                                 System.out.println("Goodbye!");
                                                 break;
                                         } else {
                                                 System.out.println("Unknown argument, please choose either " +
                                                                          "information, register, unregister or quit!");
                                                 continue;
                                         }
                                 }
                                 conn.close();
                         } 
                         catch(Exception e) { 
                        	 System.err.println( e.getMessage());
                           }
                           
                         
                 } else {
                         System.err.println("Wrong number of arguments");
                         System.exit(3);
                 }
         }

         static void getInformation(Connection conn1, String student)
         {
        try{
                 Statement stat = conn1.createStatement();
                 Statement stat1 = conn1.createStatement();

                 System.out.println ("Information :");
                 ResultSet rs = stat.executeQuery ("SELECT * FROM source_00001063");
                 while(rs.next())
                 {
                     System.out.println
                     ("----------------------------------------");
                     System.out.println ("Name: " +	rs.getString("featureid"));
                     System.out.println ("Line: " +	rs.getString("featuretype"));
                     System.out.println ("Branch: " +
                     rs.getString("method"));
                    }   
                             
                  }
         catch (SQLException e) 
         {
                 System.err.println(e);
                 System.exit(2);
          }

        }

         /*static void registerStudent(Connection conn, String student, String course)
         {
            try{
                 Statement stat = conn.createStatement();
                 Statement stat1 = conn.createStatement();
                 String my_insert = "INSERT INTO get_registered_waiting (course_code,student_ID) VALUES ('" + course + "', '" + student +"')"  ;
                 stat.executeUpdate(my_insert);
                 ResultSet rs = stat.executeQuery ("SELECT * FROM get_registered_waiting r, courses c WHERE r.course_code=c.code and code= '" + course + "' and r.student_id = '" + student + "'");
                 rs.next();
                 String Status = rs.getString("status");
                 if (Status.equals("Registered"))
                 {
                   System.out.println ("You are now successfully registered to course " + course + " " + rs.getString("name") + "!\n");
                 }
                 else
                 {
                     ResultSet rs1 = stat1.executeQuery ("SELECT ranking from (SELECT student_id, RANK() OVER (ORDER BY time ASC) as ranking  " +
                                                          "FROM waiting  where	lim_code='" +course +"') where	student_id='" + student + "'" );
                     rs1.next();
                     String ranking = rs1.getString("ranking");

                     System.out.println ("Course " + course + " " + rs.getString("name") + " is full, you are put in the waiting list as number "
                                          +ranking + "\n");
                  }

              }
         catch (SQLException e)
         {
               System.err.println(e);
               System.exit(2);
         }

         }

         static void unregisterStudent(Connection conn, String student, String course)
         {
         try{
               Statement stat = conn.createStatement();
               String my_insert = "Delete From GET_REGISTERED_WAITING Where course_code='" + course + "' and student_ID= '" + student +"'"; 
               stat.executeUpdate(my_insert);
               ResultSet rs = stat.executeQuery ("SELECT * FROM courses WHERE code= '" + course + "'");
               rs.next();
               System.out.println ("You are now successfully unregistered from the course " + course + " " +rs.getString("name") + "!\n");
              }
         catch (SQLException e)
         {
               System.err.println(e);
               System.exit(2);
         }

         }*/
 }

